Repository navigation
feat(base): add parse_fmt to turn a MATLAB-style format string into options - #321
Open
petercorke wants to merge 1 commit into
Open
petercorke wants to merge 1 commit into
petercorke wants to merge 1 commit into
Conversation
…ptions Parse strings such as 'r--', 'go' or 'C2:' into color, marker and linestyle keys, with only the parts the string sets, so the result can be merged over defaults. Follows the rules of Matplotlib's plot(): a marker with no linestyle means no line. Tested against Matplotlib's own parser. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
spatialmath.base.parse_fmt(fmt), which turns a MATLAB-style format string such as"r--","go"or"C2:"into a dict of Matplotlib options.Why. The 2D plot functions here (
plot_point,plot_polygon,plot_circle,plot_ellipse) take the string as*fmtand hand it toplt.plot. That works, but there is no way to put a default underneath it, because a defaultcolor=keyword would override the string's color. Callers that want defaults (the Machine Vision Toolbox and the Robotics Toolbox draw things in a default color and width) have each written their own parser, and they differ. This gives one that they can share.What it returns. Only the parts the string sets, as
color,markerandlinestylekeys, sodefaults | parse_fmt(fmt) | kwargsworks as expected:It follows Matplotlib's rules for
plot(): a marker with no line style means no line,C0toCnare cycle colors, a string that is a whole color ("red","#ff8000","0.5") is a color, and"1"is a marker, not a gray level. It raisesValueErrorfor two colors, two markers, two line styles or an unknown character. The one difference is that it refuses two colors when one is a cycle color ("bC0"), whichplot()accepts and quietly takes the last of.It does not accept the unusual forms that Matplotlib allows (a space as a line style,
"None", numeric markers).Tests.
tests/base/test_parse_fmt.pycompares the result with Matplotlib's own_process_plot_formatfor every color, marker and line style in every order (about 6,600 valid strings), every valid pair, whole-string colors, and a set of invalid strings that both refuse. It also checks that the result is accepted byplot()and gives the same line as the string.blackis clean andtests/basepasses (240 passed, 1 skipped).Nothing else changes: the existing plot functions are untouched. The function is added to the lazy imports in
base/__init__.pyand tofunc_2d_graphics.rst.AI-assisted: written with Claude Code (Claude Sonnet 5.5) and reviewed by the maintainer.
🤖 Generated with Claude Code